// #ifdef __THREAD_HPP__
// #define __THREAD_HPP__

#include <istream>
#include <string>
#include <functional>
#include <unistd.h>
#include <pthread.h>
#include <vector>

using namespace std;

namespace ThreadNodule
{
    template <typename T>
    using func_t = function<void(T)>;
    // typede function<void(const T&)> func_t

    template <typename T>
    class Thread
    {
    public:
        void Excute()
        {
            _func(_data);
        }

    public:
        Thread(func_t<T> func, T data, const string &name = "none-name")
            : _func(func), _data(data), _threadname(name), _stop(true)
        {
        }

        static void *threadroutine(void *args)
        {
            Thread<T> *self = static_cast<Thread<T> *>(args);
            self->Excute();

            return nullptr;
        }
        bool Start()
        {
            int n = pthread_create(&_tid, nullptr, threadroutine, this);
            if (n == 0)
            {
                _stop = false;
                return true;
            }
            else
                return false;
        }
        void Detach()
        {
            if (!_stop)
            {
                pthread_detach(_tid);
            }
        }
        void Join()
        {
            if (!_stop)
            {
                pthread_join(_tid, nullptr);
            }
        }
        void Stop()
        {
            _stop = true;
        }

        string name()
        {
            return _threadname;
        }
        pthread_t tid()
        {
            return _tid;
        }

        ~Thread() {}

    private:
        pthread_t _tid;
        string _threadname;
        T _data;
        func_t<T> _func;
        bool _stop;
    };
}

// #endif